package com.cskaoyan.exception;

import com.cskaoyan.model.vo.BaseRespVo;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import javax.naming.AuthenticationException;

/**
 * @program: project-market
 * @description: 自定义异常处理类
 * @author: fish-team
 * @create: 2021-11-29 09:23
 **/
@RestControllerAdvice
public class ExceptionControllerAdvice {


    @ExceptionHandler(TestException.class)
    public BaseRespVo parameter(TestException exception) {

        // 可以返回为JSON，也可以返回为JSP
        String message = exception.getMessage();

        return BaseRespVo.fail(message);

    }

    /**
     * 请求参数中Bigdecima转换的异常处理
     *
     * @param exception
     * @return
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public BaseRespVo httpMessageEx(HttpMessageNotReadableException exception) {

        return BaseRespVo.fail("参数异常,已驳回");

    }

    /**
     * 请求参数数字转换的异常处理
     *
     * @param exception
     * @return
     */
    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    public BaseRespVo httpMessageEx(MethodArgumentTypeMismatchException exception) {

        return BaseRespVo.manual(402, "参数值不对", null);

    }


    @ExceptionHandler(AuthorizationException.class)
    public BaseRespVo auth(AuthorizationException exception) {

        return BaseRespVo.manual(506, "权限异常，请登录高级权限账号", null);

    }

}
